﻿using DevComponents.DotNetBar;
using Kimd.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using WorkStation.Common;
using WorkStation.Logic;

namespace WorkStation.UI
{
    public partial class FormPLC : Office2007Form
    {
        //0:手动状态 1:待机状态 2:自动运行状态 3:未回原 4:回原中 5:单步状态 6:暂停状态 7:停机过程中 8:设备报警 9:急停中
        private List<string> _statusNames = new List<string>() { "手动中", "待机状态", "自动运行状态", "未回原", "回原中", "单步状态", "暂停状态", "停机过程中", "设备报警", "急停中" };
        private List<Label> _labels = new List<Label>();
        private List<Label> _labels2 = new List<Label>();
        private List<Label> _labelsText2 = new List<Label>();
        public FormPLC()
        {
            InitializeComponent();
            this.EnableGlass = false;
        }
        public void createStatusLabel()
        {
            for (int i = 0; i < 10; i++)
            {
                Label status = new Label();
                status.ImageList = iL_Input;
                status.ImageIndex = 1;
                status.ImageList = this.iL_Input;
                status.Location = new System.Drawing.Point(10 + i * 105, 14);
                status.Name = "l_Status1";
                status.Size = new System.Drawing.Size(70, 48);
                status.TabIndex = 0;
                _labels.Add(status);
                this.p_Status.Controls.Add(status);

                Label statusText = new Label();
                statusText.AutoSize = true;
                statusText.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                statusText.Location = new System.Drawing.Point(24 + i * 105, 69);
                statusText.Name = "l_StatusText1";
                statusText.Size = new System.Drawing.Size(44, 17);
                statusText.TabIndex = 1;
                statusText.Text = _statusNames[i];
                this.p_Status.Controls.Add(statusText);
            }
        }
        public void createSignalLabel()
        {
            int index = 0;
            foreach (PLCInFlag item in Enum.GetValues(typeof(PLCInFlag)))
            {
                Label status = new Label();
                status.ImageList = iL_Input;
                status.ImageIndex = 1;
                status.ImageList = this.iL_Input;
                status.Location = new System.Drawing.Point(20, 4 + index * 30);
                status.Name = "l_Status1";
                status.Size = new System.Drawing.Size(30, 30);
                status.TabIndex = 0;
                _labels2.Add(status);
                this.panelEx1.Controls.Add(status);

                Label statusText = new Label();
                statusText.AutoSize = true;
                statusText.Font = new System.Drawing.Font("微软雅黑", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                statusText.Location = new System.Drawing.Point(24+ 40, 12 + index * 30);
                statusText.Name = "l_StatusText1";
                statusText.Size = new System.Drawing.Size(30, 15);
                statusText.TabIndex = 1;
                statusText.Text = EnumHelper.GetEnumDescriptionFromValue(item);
                _labelsText2.Add(statusText);
                this.panelEx1.Controls.Add(statusText);

                index++;
            }
        }
        public void UpdateUI()
        {
            int val = 0;
            //for (int i = 0; i < GlobalVariable.PCReadPLCDataSecond.Length; i++)
            //{
            //    val += GlobalVariable.PCReadPLCDataSecond[i] ? (int)Math.Pow(2, i) : 0;
            //}
            for (int i = 0; i < _labels.Count; i++)
            {
                if(i == GlobalVariable.PLCStatusData)
                    _labels[i].ImageIndex = 0;
                else
                    _labels[i].ImageIndex = 1;
            }
            for (int i = 0; i < _labels2.Count; i++)
            {
                if(GlobalVariable.PCReadPLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCInFlag>(_labelsText2[i].Text)])
                    _labels2[i].ImageIndex = 0;
                else
                    _labels2[i].ImageIndex = 1;
            }

            bool isFirst = true;
            bool hasErr = false;
            for (int i = 0; i < GlobalVariable.PLCErrorCode.Length; i++)
            {
                if (GlobalVariable.PLCErrorCode[i])
                {
                    if (isFirst)
                    {
                        addErrInfo(EnumHelper.GetEnumDescriptionFromValue((PLCErrorCode)i), isFirst);
                        isFirst = false;
                    }
                    hasErr = true;
                }
            }
            if (!hasErr)
            {
                l_Err.Text = "无";
                l_Err.ForeColor = Color.Black;
                l_Err.BackColor = Color.FromArgb(227, 239, 255);
            }
            isFirst = true;
            hasErr = false;
            for (int i = 0; i < GlobalVariable.PLCWarnCode.Length; i++)
            {
                if (GlobalVariable.PLCWarnCode[i])
                {
                    if (isFirst)
                    {
                        addWarnInfo(EnumHelper.GetEnumDescriptionFromValue((PLCWarnCode)i), isFirst);
                        isFirst = false;
                    }
                    hasErr = true;
                }
            }
            if (!hasErr)
            {
                l_Warn.Text = "无";
                l_Warn.ForeColor = Color.Black;
                l_Warn.BackColor = Color.FromArgb(227, 239, 255);
            }
        }
        private void addErrInfo(string info, bool isFirst)
        {
            if (l_Err.Text.Contains(info))
                return;
            if (!isFirst)
            {
                l_Err.Text += "\r\n";
                l_Err.Text += info;
            }
            else
            {
                l_Err.Text = "";
                l_Err.Text += info;
            }
            l_Err.ForeColor = Color.Red;
            l_Err.BackColor = Color.Yellow;
        }
        private void addWarnInfo(string info, bool isFirst)
        {
            if (l_Warn.Text.Contains(info))
                return;
            if (!isFirst)
            {
                l_Warn.Text += "\r\n";
                l_Warn.Text += info;
            }
            else
            {
                l_Warn.Text = "";
                l_Warn.Text += info;
            }
            l_Warn.ForeColor = Color.Red;
            l_Warn.BackColor = Color.Yellow;
        }

        private void t_UI_Tick(object sender, EventArgs e)
        {
            UpdateUI();
        }

        private void FormPLC_Load(object sender, EventArgs e)
        {
            createStatusLabel();
            createSignalLabel();
        }
        object lockObj = new object();
        private void sB_AutoManual_ValueChanged(object sender, EventArgs e)
        {
            lock (lockObj)
            {
                if (sB_AutoManual.Value)
                {
                    GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("轨道手动自动切换")] = true;
                }
                else
                {
                    GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("轨道手动自动切换")] = false;
                }
                Thread.Sleep(200);
            }
        }

        private void btn_ConveyorStart_Click(object sender, EventArgs e)
        {
            GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("轨道自动启动")] = true;
            Thread.Sleep(500);
            GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("轨道自动启动")] = false;
        }

        private void btn_ConveyorHome_Click(object sender, EventArgs e)
        {
            //GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("轨道回零启动")] = true;
            //Thread.Sleep(2000);
            //GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("轨道回零启动")] = false;
        }

        private void btn_ConveyorClear_Click(object sender, EventArgs e)
        {
            GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("清除轨道故障")] = true;
            Thread.Sleep(500);
            GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("清除轨道故障")] = false;
        }

        private void btn_ConveyorHome_MouseDown(object sender, MouseEventArgs e)
        {
            GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("轨道回零启动")] = true;
        }

        private void btn_ConveyorHome_MouseUp(object sender, MouseEventArgs e)
        {
            GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("轨道回零启动")] = false;
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("人工放置满Tray")] = true;
            Thread.Sleep(500);
            GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("人工放置满Tray")] = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //lock (lockObj)
            //{
            //    bool mode = GlobalVariable.PCReadPLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCInFlag>("当前模式")];

            //    //手动中
            //    if (!mode)
            //        sB_AutoManual.Value = false;
            //    else
            //        sB_AutoManual.Value = true;
            //}
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("需上料满Tray")] = true;
            GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("需下料空Tray")] = true;
        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("需上料满Tray")] = false;
            Thread.Sleep(300);
            GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("取上料满Tray完成")] = true;
            Thread.Sleep(500);
            GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("取上料满Tray完成")] = false;

        }

        private void buttonX4_Click(object sender, EventArgs e)
        {
            GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("需下料空Tray")] = false;
            Thread.Sleep(300);
            GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("放下料空Tray完成")] = true;
            Thread.Sleep(500);
            GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("放下料空Tray完成")] = false;

        }

        private void buttonX7_Click(object sender, EventArgs e)
        {
            GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("需上料满Tray2")] = true;
            GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("需下料空Tray2")] = true;
        }

        private void buttonX6_Click(object sender, EventArgs e)
        {
            GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("需上料满Tray2")] = false;
            Thread.Sleep(300);
            GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("取上料满Tray完成2")] = true;
            Thread.Sleep(500);
            GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("取上料满Tray完成2")] = false;
        }

        private void buttonX5_Click(object sender, EventArgs e)
        {
            GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("需下料空Tray2")] = false;
            Thread.Sleep(300);
            GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("放下料空Tray完成2")] = true;
            Thread.Sleep(500);
            GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("放下料空Tray完成2")] = false;
        }
    }
}
